﻿namespace Huayou.Common.Database
{
    using System;
    using System.Data;
    using System.Data.Odbc;

    public class ODBCDBOperate : DBOperate
    {
        private bool bInTrans = false;
        private OdbcConnection conn;
        private OdbcTransaction trans;

        public ODBCDBOperate(string strConnection)
        {
            this.conn = new OdbcConnection(strConnection);
        }

        public override void BeginTran()
        {
            if (!this.bInTrans)
            {
                this.trans = this.conn.BeginTransaction();
                this.bInTrans = true;
            }
        }

        public override void Close()
        {
            if (this.conn.State.Equals(ConnectionState.Open))
            {
                this.conn.Close();
            }
        }

        public override void CommitTran()
        {
            if (this.bInTrans)
            {
                this.trans.Commit();
                this.bInTrans = false;
            }
        }

        public override void Dispose()
        {
            this.Dispose(true);
            GC.SuppressFinalize(this);
        }

        protected void Dispose(bool disposing)
        {
            if (disposing)
            {
                if (this.bInTrans && (this.trans != null))
                {
                    this.trans = null;
                }
                if (this.conn != null)
                {
                    if (this.conn.State == ConnectionState.Open)
                    {
                        this.conn.Close();
                    }
                    this.conn.Dispose();
                    this.conn = null;
                }
            }
        }

        public override IDataReader ExecPreQueryForDataReader(string strSql, IDataParameter[] parameters)
        {
            return null;
        }

        public override DataSet ExecPreQueryForDataSet(string strSql, IDataParameter[] parameters, string strTableName)
        {
            return null;
        }

        public override DataTable ExecPreQueryForDataTable(string strSql, IDataParameter[] parameters)
        {
            return null;
        }

        public override int ExecPreUpdateSql(string strSql, IDataParameter[] parameters)
        {
            return 0;
        }

        public override IDataReader ExecProcForDataReader(string strSql, IDataParameter[] parameters)
        {
            return null;
        }

        public override DataSet ExecProcForDataSet(string strSql, IDataParameter[] parameters, string strTableName)
        {
            return null;
        }

        public override DataTable ExecProcForDataTable(string strSql, IDataParameter[] parameters)
        {
            return null;
        }

        public override IDataReader ExecQueryForDataReader(string strSql)
        {
            return null;
        }

        public override DataSet ExecQueryForDataSet(string strSql, string strTableName)
        {
            return null;
        }

        public override DataTable ExecQueryForDataTable(string strSql)
        {
            DataTable dataTable = new DataTable();
            OdbcDataAdapter adapter = new OdbcDataAdapter(strSql, this.conn);
            OdbcCommand command = this.conn.CreateCommand();
            command.CommandText = strSql;
            if (this.bInTrans)
            {
                command.Transaction = this.trans;
            }
            adapter.SelectCommand = command;
            adapter.Fill(dataTable);
            return dataTable;
        }

        public override object ExecScalar(string strSql)
        {
            throw new Exception("The method or operation is not implemented.");
        }

        public override object ExecScalar(string strSql, IDataParameter[] parameters)
        {
            throw new Exception("The method or operation is not implemented.");
        }

        public override int ExecUpdateProc(string strSql, IDataParameter[] parameters)
        {
            return 0;
        }

        public override int ExecUpdateSql(string strSql)
        {
            return 0;
        }

        private OdbcCommand GetPreCommand(string strSql, IDataParameter[] parameters, string strCommandType)
        {
            OdbcCommand command = this.conn.CreateCommand();
            try
            {
                try
                {
                    command.CommandText = strSql;
                    if (strCommandType == "PROCEDURE")
                    {
                        command.CommandType = CommandType.StoredProcedure;
                    }
                    if (this.bInTrans)
                    {
                        command.Transaction = this.trans;
                    }
                    foreach (IDataParameter parameter in parameters)
                    {
                        command.Parameters.Add(parameter);
                    }
                }
                catch (Exception exception)
                {
                    throw exception;
                }
            }
            finally
            {
            }
            return command;
        }

        public override void Open()
        {
            if (this.conn.State.Equals(ConnectionState.Closed))
            {
                this.conn.Open();
            }
        }

        public override void RollBackTran()
        {
            if (this.bInTrans)
            {
                this.trans.Rollback();
                this.bInTrans = false;
            }
        }

        public override IDbConnection baseConnection
        {
            get
            {
                return this.conn;
            }
            set
            {
                this.conn = (OdbcConnection) value;
            }
        }

        public override IDbTransaction baseTransaction
        {
            get
            {
                return this.trans;
            }
            set
            {
                this.trans = (OdbcTransaction) value;
            }
        }
    }
}

